package com.lmt.service.impl;

import com.lmt.base.BaseService;
import com.lmt.base.ResultData;
import com.lmt.entity.CollectionFolder;
import com.lmt.entity.search.CollectionFolderSearch;
import com.lmt.repository.CollectionFolderRepository;
import com.lmt.service.CollectionFolderService;
import com.lmt.util.ResultUtil;
import com.lmt.util.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Date 2018/07/17
 *
 * @author MT-Lin
 */
@Service("CollectionFolderServiceImpl")
public class CollectionFolderServiceImpl extends BaseService implements CollectionFolderService {

	private final CollectionFolderRepository collectionFolderRepository;

	@Autowired
	public CollectionFolderServiceImpl(CollectionFolderRepository collectionFolderRepository) {
		this.collectionFolderRepository = collectionFolderRepository;
	}

	@Override
	public ResultData save(CollectionFolder collectionFolder) {
		CollectionFolder exist = this.collectionFolderRepository.findByCreatorAndDesignationAndParentId(
				collectionFolder.getCreator(), collectionFolder.getDesignation(), collectionFolder.getParentId());
		if (null != exist) {
			return ResultUtil.newFail("所属收藏夹下已有该名称的收藏夹");
		}
		this.collectionFolderRepository.save(collectionFolder);
		if (null == collectionFolder.getId()) {
			return ResultUtil.newFail("收藏夹创建失败");
		}
		return ResultUtil.newSuccess("收藏夹创建成功");
	}

	@Override
	public ResultData search(CollectionFolderSearch collectionFolderSearch) {
		StringBuffer sql = new StringBuffer();
		List<Object> parameters = new ArrayList<>();
		sql.append("SELECT cf.id, cf.designation, cf.description, cf.creator, ");
		sql.append("cf.parent_id AS parentId, ");
		sql.append("cf.gmt_create AS gmtCreate, ");
		sql.append("cf.resource_num AS resourceNum, ");
		sql.append("(SELECT cft.designation FROM collection_folder AS cft WHERE cft.id = cf.parent_id) ");
		sql.append("AS parent ");
		sql.append("FROM collection_folder AS cf ");
		sql.append("WHERE 1 = 1 ");
		if (!StringUtils.isEmpty(collectionFolderSearch.getDesignation())) {
			sql.append("AND cf.designation = ? ");
			parameters.add(collectionFolderSearch.getDesignation());
		}
		sql.append("ORDER BY cf.gmt_create DESC ");
		Map<String, Object> result = this.paginationSearch(sql, collectionFolderSearch, parameters);
		return ResultUtil.newSuccess("收藏夹信息查询成功", result);
	}

	@Override
	public ResultData getParentTree() {
		List result = this.getOptions();
		return ResultUtil.newSuccess("成功获取父节点选择树", result);
	}

	@Override
	public ResultData delete(List<CollectionFolder> collectionFolders) {
		for (CollectionFolder collectionFolder : collectionFolders) {
			List<CollectionFolder> children = this.collectionFolderRepository.findByParentId(collectionFolder.getId());
			if (children.size() > 0) {
				this.delete(children);
			}
			this.collectionFolderRepository.deleteById(collectionFolder.getId());
		}
		return ResultUtil.newSuccess("成功删除收藏夹");
	}

	@Override
	public ResultData update(CollectionFolder collectionFolder) {
		Optional<CollectionFolder> exist = this.collectionFolderRepository.findById(collectionFolder.getId());
		if (!exist.isPresent()) {
			return ResultUtil.newFail("修改的收藏夹不存在");
		}
		this.collectionFolderRepository.save(collectionFolder);
		return ResultUtil.newSuccess("收藏夹修改成功");
	}

	private List getOptions() {
		List<Map<String, Object>> result = new ArrayList<>();
		List<CollectionFolder> firstLevel = new ArrayList<>();
		List<CollectionFolder> children = new ArrayList<>();
		List<String> parentId = new ArrayList<>();
		List<CollectionFolder> all = this.collectionFolderRepository.findAll();
		Map<String, Map<String, Object>> temp = new HashMap<>(all.size());
		for (CollectionFolder collectionFolder : all) {
			Map<String, Object> tempOption = new HashMap<>(9);
			tempOption.put(SystemConstant.PROPS_VALUE, collectionFolder.getId());
			tempOption.put(SystemConstant.PROPS_ID, collectionFolder.getId());
			tempOption.put(SystemConstant.PROPS_LABEL, collectionFolder.getDesignation());
			tempOption.put(SystemConstant.PROPS_CHILDREN, new ArrayList<>());
			temp.put(collectionFolder.getId(), tempOption);
			if (collectionFolder.getParentId() != null) {
				parentId.add(collectionFolder.getParentId());
			}
			if (null == collectionFolder.getParentId()) {
				firstLevel.add(collectionFolder);
			} else {
				children.add(collectionFolder);
			}
		}
		for (CollectionFolder child : children) {
			//noinspection unchecked
			((List) temp.get(child.getParentId()).get(SystemConstant.PROPS_CHILDREN)).add(temp.get(child.getId()));
			if (!parentId.contains(child.getId())) {
				temp.get(child.getId()).remove(SystemConstant.PROPS_CHILDREN);
			}
		}
		for (CollectionFolder collectionFolder : firstLevel) {
			result.add(temp.get(collectionFolder.getId()));
		}
		return result;
	}
}
